package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {


    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 添加科室信息的实现
     * @param stringObjectMap
     */
    @Override
    public void save(Map<String, Object> stringObjectMap) {

        // stringObjectMap转换成Department对象
        String stringObjectMapString = JSONObject.toJSONString(stringObjectMap);
        Department department = JSONObject.parseObject(stringObjectMapString, Department.class);

        //根据医院编号和科室编号查询数据
       Department departmentExit =
               departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

       if(departmentExit!=null){
           departmentExit.setUpdateTime(new Date());
           departmentExit.setIsDeleted(0);
           // mongo自带的save方法
           departmentRepository.save(departmentExit);
       }
       department.setCreateTime(new Date());
       department.setUpdateTime(new Date());
       department.setIsDeleted(0);
        departmentRepository.save(department);

    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建pageable对象
        // mongo中0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {

        // 先根据医院编号和科室编号查询是否有数据
        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(departmentByHoscodeAndDepcode!=null){
            departmentRepository.deleteById(departmentByHoscodeAndDepcode.getId());
        }

    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        // 创建List集合，用于封装数据
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号,查询医院所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example example = Example.of(department);
        // 所有科室列表信息,里面有大科室和小科室
        List<Department> departmentList = departmentRepository.findAll();

        //根据大科室编号 bigcode分组,然后再根据大科室中的下级科室
        // jsk8中流的方式进行分组
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 遍历map集合
        for(Map.Entry<String, List<Department>> entry : departmentMap.entrySet()){
            // 大科室编号
            String bigcode = entry.getKey();
            // 大科室编号对应的全局数据,包括小科室
            List<Department> department1List = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(department1List.get(0).getBigname());

            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department1:department1List){
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department1.getDepcode());
                departmentVo2.setDepname(department1.getDepname());
                // 封装到list
                children.add(departmentVo2);
            }
            // 把小科室的集合放到大科室的集合中
            departmentVo1.setChildren(children);
            //放到最终result
            result.add(departmentVo1);

        }
        return result;
    }

    // 根据医院编号 科室编号查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            return department.getDepname();
        }

        return null;
    }

    // 根据医院编号 科室编号查询科室名称
    @Override
    public Department getDepartment(String hoscode, String depcode) {

        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
